// pages/index/list.js
const app = getApp()
const Util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationPop: {},
    popLogin: {},
    is_show: false,
    cate_id:0,
    is_recomment:0
  },
  home:function(){
    wx.navigateTo({
      url: 'index'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    console.info(e.cate_id);
    if(e.cate_id){
      that.setData({
        cate_id:e.cate_id
      })
    }
    if (e.is_recomment){
      that.changeRec();
    }else{
      that.getGoodsList();
    }
    that.getCateList();
    that.getMember();
  },
  process: function () {
    wx.navigateTo({
      url: 'about?id=1'
    })
  },
  cooperation: function () {
    wx.navigateTo({
      url: 'contact?id=2'
    })
  },
  changeCate:function(e){
    var that = this;
    var cate_id = e.currentTarget.dataset.id;
    that.setData({
      cate_id:cate_id,
      is_recomment:0
    })
    that.getGoodsList();
  },
  changeRec: function (e) {
    var that = this;
    that.setData({
      cate_id:'-1',
      is_recomment:1
    })
    Util.getGoodsList({
      is_rec:1
    }, function (re) {
      that.setData({
        goods_list: re.data
      })
    })
  },
  goodsDetail:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'detail?id=' + id
    })
  },
  getGoodsList:function(){
    var that = this;
    var cate_id = that.data.cate_id;
    Util.getGoodsList({
      cate_id:cate_id
    }, function (re) {
      that.setData({
        goods_list: re.data
      })
    })
  },
  getCateList: function () {
    var that = this;
    Util.getAllCateList({}, function (re) {
      that.setData({
        cate_list: re.data
      })
    })
  },
  getMember: function () {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        if (res.data) {
          that.setData({
            token: res.data,
            is_show: true
          });
        }
      }, fail: function () {
        that.checkMember();
      }
    })
  },
  checkMember: function () {
    var that = this;
    that.checkUser(-1, function (isauth) {
      console.info(isauth);
      if (isauth) {
        that.setData({
          is_show: true
        })
      } else {
        mytoast.toast();
        Util.showMsgDialog("请先授权再使用");
      }
    })
  },
  checkUser: function (action, cb) {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        console.info(res);
        if (res.data) {
          cb(true);
        }
      },
      fail: function () {
        Util.checkAuth(function (res) {
          if (!res.authSetting['scope.userInfo']) {
            //未授权用户
            that.showLogin();
          } else {
            app.getUserInfo(true, function (userInfo) {

              if (typeof cb == "function") {
                cb(true);
              }
            });
          }
        });
      }
    })
  },
  onGotUserInfo: function (e) {
    var that = this;
    if (e.detail.errMsg == 'getUserInfo:ok') {
      Util.showLoading('授权中');
      app.getUserInfo(true, function (userInfo) {
        if (typeof cb == "function") {
          cb(true);
        }
        that.setData({
          is_show: true
        });
        that.getMember();
      });
    }
    that.setData({
      popLoginStatus: false
    })
  },
  setPopStatus: function (e) {
    this.showPop(e.currentTarget.dataset.status);
  },
  showLogin: function () {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.scale(0).step()

    this.setData({
      popLogin: animation.export(),
      popLoginStatus: true
    })

    setTimeout(function () {
      animation.scale(1.0).step()
      this.setData({
        popLogin: animation
      })
    }.bind(this), 100)
  },
  showPop: function (status) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.scale(0).step()

    this.setData({
      animationPop: animation.export()
    })
    if (status == 1) {
      this.setData(
        {
          showPopStatus: true
        }
      );
    }
    setTimeout(function () {
      animation.scale(1.0).step()
      this.setData({
        animationPop: animation
      })
      if (status == 0) {
        this.setData(
          {
            showPopStatus: false,
          }
        );
      }
    }.bind(this), 300)
  },
  detail:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'detail?id='+id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})